const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listCol: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  onShow:function(){

    var nickname = wx.getStorageSync('accountInfo').nickname;
    this.setData({
      nickname: nickname
    })

  },

  deleteSearch: function () {
    this.setData({
      searchText: '',
      listCol: true
    })
  },

  searchInput: function (e) {
    var keyWord = e.detail.value;
    // 假设现在需要检测到用户输入的值，用户 400 毫秒内没有继续输入就将该值打印出来
    this.throttle(this.getSearchList, null, 400, keyWord);
  },

  // 节流
  throttle: function (fn, context, delay, text) {
    clearTimeout(fn.timeoutId);
    fn.timeoutId = setTimeout(function () {
      fn.call(context, text);
    }, delay);
  },
  // 想要执行的函数
  getSearchList: function (text) {
    var that = this;
    if (text) {
      util.showLoading("加载中...");
      var params = new Object()
      var that = this;
      params.nikename = text;
      params.type=2;
      api.POST({
        params: params,
        url: app.globalData.base_url + 'askWine/searchFriends',
        success: function (res) {
          wx.hideLoading();
          if (util.isEmpty(res.data.code)) {
            if (res.data.data.length > 0) {
              var searchList = res.data.data
            } else {
              var searchList = []
            }
            that.setData({
              listCol: false,
              searchList: searchList
            })
          }
        },
        fail: function (fail) {
          util.showError("网络错误，请检查网络");
        }
      })

    } else {
      this.setData({
        listCol: true
      })
    }
  },

  addFriend:function(e){
    var index = e.currentTarget.dataset.index;
    var uid = e.currentTarget.dataset.uid;
    var that = this;
    util.showLoading("加载中...");
    var params = new Object()
    var that = this;
    params.uid = uid;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'askWine/addFriends',
      success: function (res) {
        wx.hideLoading();
        util.showSuccess("添加成功");
        if (util.isEmpty(res.data.code)) {
          that.data.searchList[index].is_friend = '1';
        }
        that.setData({
          searchList: that.data.searchList
        })
      },
      fail: function (fail) {
        wx.hideLoading();
        util.showError("网络错误，请检查网络");
      }
    })

  },
  onShareAppMessage: function (res) {
    var that = this;
    var share_uid = wx.getStorageSync('accountInfo').uid;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: this.data.nickname + '向您分享了飘香酒掌柜小程序',
        path: '/pages/index/index?share_uid=' + share_uid,
        imageUrl: '/images/share.jpg',
        success: function (res) {
          // 转发成功
        },
        fail: function (res) {
          // 转发失败
        }
      }
    }
  },

})